package com.weipu.common.subject;

import com.weipu.common.observer.RedDotObserver;

import java.util.ArrayList;
import java.util.List;

/**
 * 小红点 被观察者实现类
 * @author Zenglw
 * @version [1.0,2017/6/14]
 */

public class RedDotSubjectImpl implements RedSubject {

    private static RedDotSubjectImpl instance;

    private List<RedDotObserver> mObservers;

    private RedDotSubjectImpl() {
        mObservers = new ArrayList<>();
    }

    public static RedDotSubjectImpl getInstance() {
        if (instance == null) {
            synchronized (RedDotSubjectImpl.class) {
                if (instance == null) {
                    instance = new RedDotSubjectImpl();
                }
            }
        }
        return instance;
    }

    /**
     * 注册观察者
     * @param observer
     */
    @Override
    public void registerObserver(RedDotObserver observer) {
        mObservers.add(observer);
    }

    /**
     * 注册被观察者
     * @param observer
     */
    @Override
    public void unRegisterObserver(RedDotObserver observer) {
        mObservers.remove(observer);
    }

    /**
     * 通知观察者
     */
    @Override
    public void notifyObserver() {
        if (mObservers.size() > 0) {
            for (RedDotObserver observer: mObservers) {
                observer.noticeHasRead();
            }
        }
    }
}
